package com.zhike.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhike
 * @CreateTime: 2024/5/1
 * @Description: 柱形图相关工具类
 */
public class BarUtils {
    public static String getDate(String filter) {
        JSONObject jsonObject = JSON.parseObject(filter);
        String conditions = jsonObject.getJSONArray("conditions").get(0).toString();
        //获取过滤日期
        String date = JSON.parseObject(conditions).getString("v");
        return date;
    }

    public static Map getDetail(List<Map> avgSpeedToday, String dim,String field1,String field2, String label1,String label2) {
        List<String> allVin = new ArrayList<>();
        List<Double> allAvg = new ArrayList<>();
        List<Double> allGeneral = new ArrayList<>();
        List<Map> series = new ArrayList<>();

        if (!avgSpeedToday.isEmpty()){
            for (Map map : avgSpeedToday) {
                allVin.add(map.get(dim).toString());
                allAvg.add(Double.parseDouble(map.get(field1).toString()));
                allGeneral.add(Double.parseDouble(map.get(field2).toString()));
            }
        }
        Map desc = new HashMap();
        desc.put("name",label1);
        desc.put("data",allAvg);
        desc.put("type","line");
        series.add(desc);

        Map descs = new HashMap();
        descs.put("name",label2);
        descs.put("data",allGeneral);
        descs.put("type","bar");
        series.add(descs);

        Map data=new HashMap();
        data.put("categories",allVin);
        data.put("series",series);
        return data;
    }

    public static Map getDetailSingle(List<Map> dataList,String dim,String field, String label) {
        List<String> allVin = new ArrayList<>();
        List<Double> allCount = new ArrayList<>();
        List<Map> series = new ArrayList<>();
        if (!dataList.isEmpty()){
            for (Map map : dataList) {
                allVin.add(map.get(dim).toString());
                allCount.add(Double.parseDouble(map.get(field).toString()));
            }
        }
        Map desc = new HashMap();
        desc.put("name",label);
        desc.put("data",allCount);
        series.add(desc);
        Map data = new HashMap();
        data.put("categories",allVin);
        data.put("series",series);
        return data;
    }
}
